package com.boot.pojo;

import com.boot.utils.MD5Util;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Arrays;
import java.util.Collection;
import java.util.stream.Collectors;

@Data
public class CustomUserDetail implements UserDetails {
    private Long id;
    private String username;
    private String password;
    private String roles;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if (roles == null || roles.isEmpty()) {
            return null;
        }
        return Arrays.stream(roles.split(","))
                .map(role -> new SimpleGrantedAuthority("ROLE_" + role))
                .collect(Collectors.toList());
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return username;
    }

    //判断账户是否过期
    //如return this.isExpired;
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    //判断账户是否未锁定
    //如return !this.locked;
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    //密码证书登更换策略
    //如return this.passwordLastChanged.plusMonths(3).isAfter(LocalDate.now());
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    //账户是否启用
    //如return this.enable == 1 ? true : false;
    @Override
    public boolean isEnabled() {
        return true;
    }
}

